package ar.unq.edu.persistencia.persistencia;

import java.util.List;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.query.Predicate;
import com.db4o.query.Query;

/**
 * TODO: Almacena y recupera datos de la base
 */
public class DBManager {
    private static DBManager inst = new DBManager();

    private static final String FILE_NAME = "../baseDeDatos.db4o";

    private ObjectContainer container;

    // *******************//
    // ** Constructores **//
    // *******************//
    private DBManager() {
        this.initialize();
    }

    public void initialize() {
        this.container = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), FILE_NAME);
    }

    // *************************//
    // ** Getters and Setters **//
    // *************************//
    public ObjectContainer getContainer() {
        return this.container;
    }

    public static DBManager getInstance() {
        return inst;
    }

    // *************//
    // ** Metodos **//
    // *************//
    public void runOperation(final Runnable operation) {
        operation.run();
    }

    public void set(final Object obj) {
        this.container.store(obj);
    }

    /**
     * recibe una clase por parametro.
     */
    public List<?> getAll(final Class<?> clase) {
        return this.container.query(clase);
    }

    public List<?> query(final Predicate<?> condicion) {
        return this.container.query(condicion);
    }

    public List<Object> get(final Object obj) {
        return this.container.queryByExample(obj);
    }

    public void close() {
        this.container.close();
    }

    public void delete(final Object obj) {
        this.container.delete(obj);
    }

    public Query query() {
        return this.container.query();
    }

}
